﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System.Collections.Generic;
using System.Linq;

namespace UtilLib
{
    /// <summary>
    /// 公共函数类  Dictionary 嵌套 Dictionary
    /// </summary>
    public class DictDict<TKeyBase, TKey, TValue> : Dictionary<TKeyBase, Dictionary<TKey, TValue>>
    {
        /// <summary>
        /// 
        /// </summary>
        public DictDict() 
            : base()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keybase"></param>
        /// <param name="values"></param>
        /// <param name="newDict"></param>
        /// <returns></returns>
        public bool TryGetValues(TKeyBase keybase, out Dictionary<TKey, TValue> values, bool newDict = true)
        {
            bool result = TryGetValue(keybase, out values);
            if (newDict && !result)
            {
                values = new Dictionary<TKey, TValue>();
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keybase"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetValueValue(TKeyBase keybase, TKey key, TValue value)
        {
            if (!TryGetValues(keybase, out var values))
            {
                base[keybase] = values;
            }
            values[key] = value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keybase"></param>
        /// <param name="key"></param>
        public void Remove(TKeyBase keybase, TKey key)
        {
            if (!TryGetValues(keybase, out var values))
            {
                return;
            }
            values.Remove(key);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keybase"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValueValue(TKeyBase keybase, TKey key, out TValue value)
        {
            if (TryGetValues(keybase, out var values))
            {
                return values.TryGetValue(key, out value);
            }
            value = default(TValue);
            return false;
        }

    }
}